using ERPBackend.Models;
using ERPBackend.Services;
using Microsoft.AspNetCore.Mvc;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace ERPBackend.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class SalesContractsController : ControllerBase
    {
        private readonly ISalesContractService _salesContractService;

        public SalesContractsController(ISalesContractService salesContractService)
        {
            _salesContractService = salesContractService;
        }

        // GET: api/SalesContracts
        [HttpGet]
        public async Task<ActionResult<IEnumerable<SalesContract>>> GetSalesContracts()
        {
            var salesContracts = await _salesContractService.GetAllSalesContractsAsync();
            return Ok(salesContracts);
        }

        // GET: api/SalesContracts/5
        [HttpGet("{id}")]
        public async Task<ActionResult<SalesContract>> GetSalesContract(int id)
        {
            var salesContract = await _salesContractService.GetSalesContractByIdAsync(id);
            if (salesContract == null)
            {
                return NotFound();
            }
            return Ok(salesContract);
        }

        // POST: api/SalesContracts
        [HttpPost]
        public async Task<ActionResult<SalesContract>> PostSalesContract(SalesContract salesContract)
        {
            try
            {
                var createdSalesContract = await _salesContractService.CreateSalesContractAsync(salesContract);
                return CreatedAtAction("GetSalesContract", new { id = createdSalesContract.Id }, createdSalesContract);
            }
            catch (Exception ex)
            {
                return BadRequest(ex.Message);
            }
        }

        // PUT: api/SalesContracts/5
        [HttpPut("{id}")]
        public async Task<IActionResult> PutSalesContract(int id, SalesContract salesContract)
        {
            if (id != salesContract.Id)
            {
                return BadRequest("ID不匹配");
            }

            try
            {
                var result = await _salesContractService.UpdateSalesContractAsync(salesContract);
                if (!result)
                {
                    return NotFound();
                }
                return NoContent();
            }
            catch (Exception ex)
            {
                return BadRequest(ex.Message);
            }
        }

        // PUT: api/SalesContracts/5/approve
        [HttpPut("{id}/approve")]
        public async Task<IActionResult> ApproveSalesContract(int id, [FromQuery] int approvedBy)
        {
            try
            {
                var result = await _salesContractService.ApproveSalesContractAsync(id, approvedBy);
                if (!result)
                {
                    return NotFound();
                }
                return NoContent();
            }
            catch (Exception ex)
            {
                return BadRequest(ex.Message);
            }
        }

        // PUT: api/SalesContracts/5/terminate
        [HttpPut("{id}/terminate")]
        public async Task<IActionResult> TerminateSalesContract(int id, [FromQuery] string reason, [FromQuery] int terminatedBy)
        {
            try
            {
                var result = await _salesContractService.TerminateSalesContractAsync(id, terminatedBy, reason);
                if (!result)
                {
                    return NotFound();
                }
                return NoContent();
            }
            catch (Exception ex)
            {
                return BadRequest(ex.Message);
            }
        }

        // DELETE: api/SalesContracts/5
        [HttpDelete("{id}")]
        public async Task<IActionResult> DeleteSalesContract(int id)
        {
            try
            {
                var result = await _salesContractService.DeleteSalesContractAsync(id);
                if (!result)
                {
                    return NotFound();
                }
                return NoContent();
            }
            catch (Exception ex)
            {
                return BadRequest(ex.Message);
            }
        }

        // GET: api/SalesContracts/search?keyword=xxx&customerId=xxx&status=xxx
        [HttpGet("search")]
        public async Task<ActionResult<IEnumerable<SalesContract>>> SearchSalesContracts(
            [FromQuery] string keyword = null,
            [FromQuery] int? customerId = null,
            [FromQuery] int? status = null)
        {
            var salesContracts = await _salesContractService.SearchSalesContractsAsync(keyword, customerId, status);
            return Ok(salesContracts);
        }
    }
}